var Tools = {
    isAndroid: function() {
        var isAndroid = (window.navigator.userAgent.indexOf('Android') >= 0) ? true : false;
        return isAndroid;
    },
    tzThirdAPP: function(data) {
        var isAndroid = (window.navigator.userAgent.indexOf('Android') >= 0) ? true : false;
        if (isAndroid) {
            checkIsAppInstalled2(data);
            //检测是否Android版应用
        } else {
            checkIsAppInstalled(data);
            //检测是否ios版应用
        }
    }
}

//检查是否安装了ios第三方应用
function checkIsAppInstalled(info) {
    var store_appName = info.iosPkg;
    // var store_appUrl = info.pkgUrl;
    uexWidget.cbIsAppInstalled = function(data) {
        var result = JSON.parse(data);
        if (result.installed == 0) {
            var appInfo = info.iosInfo + info.text;
            appInfo = encodeURI(appInfo); //转编码
            uexWidget.loadApp(appInfo);
        } else {
            // appcan.window.confirm("温馨提示", "未安装应用,请前往安装。", ["确定", "取消"], function(err, data, dataType, optId) {
            //     if (data == 0) {
            //         //window.location.href=store_appUrl;
            //         uexWidget.loadApp(store_appUrl, '', '');
            //     }
            // })
        }
    }
    var param1 = {
        appData: store_appName
    };
    uexWidget.isAppInstalled(JSON.stringify(param1));
}
//检查是否安装了android第三方应用
function checkIsAppInstalled2(info) {
    var store_appName = info.pkgName;
    // var store_appUrl = info.pkgUrl;
    uexWidget.cbIsAppInstalled = function(data) {
        var result = JSON.parse(data);
        if (result.installed == 0) {
            loadThirdAPP(info);
        } 
        // else {
        //     appcan.window.confirm("温馨提示", "未安装应用,请前往安装。", ["确定", "取消"], function(err, data, dataType, optId) {
        //         if (data == 0) {
        //             //window.location.href = store_appUrl; 
        //             //window.location.href = "https://pcm.powerchina.cn/uploads/pkgFiles/000004_1462599859762.apk";
        //             uexWidget.loadApp('android.intent.action.VIEW', 'text/html', store_appUrl);
        //         }
        //     })
        // }
    }
    var param1 = {
        appData: info.pkgName
    };
    uexWidget.isAppInstalled(JSON.stringify(param1));
}
//启动第三方应用
function loadThirdAPP(data) {
    var packageName = data.pkgName; //应用包名
    var className = "org.zywx.wbpalmstar.engine.EBrowserActivity";
    var appInfo = data.appInfo + data.text;
    appInfo = encodeURI(appInfo); //转编码
    var extra = {
        "isNewTask": "1",
        data: appInfo
    };
    uexWidget.startApp(0, packageName, '', JSON.stringify(data.optinfo), JSON.stringify(extra));
}

function getInstalledApks(apps,cb) {
    var apps_Len = apps.length;
    var installedApks = [];
    isAppInstalled(apps,0,function(result,index) {
        if (result == 0)
            cb(apps[index]);
        else
            cb();
    })
}

function isAppInstalled(apps,index,cb) {
    var param1 = {
        appData: Tools.isAndroid() ? apps[index].pkgName : apps[index].iosPkg
    };
    uexWidget.cbIsAppInstalled = function(data) {
        var result = JSON.parse(data);
        if (result.installed == 0 || index == (apps.length -1 ))
            cb(result.installed,index);
        else{
            index ++;
            isAppInstalled(apps,index,cb);
        }
    }

    uexWidget.isAppInstalled(JSON.stringify(param1));
}